<template>
    <view class="container">
        <view class="example">
            <uni-forms ref="form" :model="user" labelWidth="80px">
                <uni-forms-item label="用户昵称" name="nickname">
                    <uni-easyinput v-model="user.nickname" placeholder="请输入昵称"/>
                </uni-forms-item>
                <uni-forms-item label="手机号码" name="mobile">
                    <uni-easyinput v-model="user.mobile" placeholder="请输入手机号码" :disabled="true"/>
                </uni-forms-item>
                <uni-forms-item label="年龄" name="age">
                    <uni-easyinput v-model="user.age" placeholder="请输入年龄"/>
                </uni-forms-item>
                <uni-forms-item label="性别" name="gender" required>
                    <uni-data-checkbox v-model="user.gender" :localdata="sexs"/>
                </uni-forms-item>
                <uni-forms-item label="生日" name="birthday">
                    <uni-datetime-picker type="date" :clear-icon="false" v-model="user.birthday" />
                </uni-forms-item>
            </uni-forms>
            <button type="primary" @click="submit">提交</button>
        </view>
    </view>
</template>

<script>
import {updateUserProfile} from "@/api/system/user"
import {mapMutations} from 'vuex'
export default {
    data() {
        return {
            user: {
                nickname: "",
                mobile: "",
                age: "",
                gender: ""
            },
            sexs: [{
                text: '男',
                value: 1
            }, {
                text: '女',
                value: 2
            }],
            rules: {
                nickname: {
                    rules: [{
                        required: true,
                        errorMessage: '用户昵称不能为空'
                    }]
                },
                mobile: {
                    rules: [{
                        required: true,
                        errorMessage: '手机号码不能为空'
                    }, {
                        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                        errorMessage: '请输入正确的手机号码'
                    }]
                },
                age: {
                    rules: [{
                        required: true,
                        errorMessage: '年龄不能为空'
                    }]
                }
            }
        }
    },
    onLoad() {
        const userInfo = uni.getStorageSync('userInfo')
        this.user = {
            ...userInfo
        }
    },
    onReady() {
        this.$refs.form.setRules(this.rules)
    },
    methods: {
        ...mapMutations(['SET_NAME']),
        submit(ref) {
            this.$refs.form.validate().then(res => {
                this.user.uid = this.user.id
                delete this.user.id
                delete this.user['session_id']
                updateUserProfile(this.user).then(response => {
                    if(response.code == 200){
                        this.SET_NAME(this.user.nickname)
                        this.$modal.msgSuccess("修改成功")
                    }
                    else{
                        this.$modal.msgError(response.message || "修改失败")
                    }

                })
            })
        }
    }
}
</script>

<style lang="scss" scoped>
page {
  background-color: #ffffff;
}

.example {
  padding: 15px;
  background-color: #fff;
}

.segmented-control {
  margin-bottom: 15px;
}

.button-group {
  margin-top: 15px;
  display: flex;
  justify-content: space-around;
}

.form-item {
  display: flex;
  align-items: center;
  flex: 1;
}

.button {
  display: flex;
  align-items: center;
  height: 35px;
  line-height: 35px;
  margin-left: 10px;
}
</style>
